import { createRouter, createWebHashHistory } from 'vue-router';

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            redirect: '/login',
        },
        {
            path: '/home',
            component: () => import('../views/Home.vue'),
            meta: {
                isAuth: true,//需要登陆权限
            },
        },
        {
            path: '/login',
            component: () => import('../views/Login.vue'),
        },
        {
            path: '/register',
            component: () => import('../views/Register.vue'),
        },
    ],
});

// 路由拦截
router.beforeEach((to, from, next) => {
    if(to.meta.isAuth){
        if(localStorage.getItem('token')){
            next();
        }else{
            ElMessage.primary('请先登录')
        }
    }else{
        next();
    }
})
export default router;